package com.example.demo.utils.wx;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static com.example.demo.utils.HttpUtils.doPostJson;

public class KefuSendUtils {

    private static final Logger logger = LoggerFactory.getLogger(KefuSendUtils.class);

    /**
     * 文章推送
     */
    public static boolean wenZhangSendByOpenId(String opendid, String jsonDesc) throws IOException {
        JSONObject jsonObject = go(opendid, jsonDesc);
        if (jsonObject.get("errcode").toString().equals("0")) {
            logger.info("-------------------定时推送推送文章或动画 success-"+ opendid +"-------------------");
            return true;
        } else {
            logger.error("openId: "+ opendid +" \t文章推送失败：" + jsonObject.get("errmsg").toString());
            return false;
        }
    }

    /**
     * 文章推送填入文章信息
     */
    public static String wenZhangSendByDesc(String title, String describe, String link, String pic) {
        return WxKefuJson.TUWENJSON.replace("TITLE", title).replace("DESCRIBE", describe).replace("URL", link).replace("PIC", pic);
    }

    /**
     * 客服消息推送
     */
    public static boolean keFuSendByOpenId(String opendid, String desc) throws IOException {
        JSONObject jsonObject = go(opendid, desc);
        if (jsonObject.get("errcode").toString().equals("0")) {
            logger.info("-------------------推送文字 success ---"+ opendid +"-----------------");
            return true;
        } else {
            logger.error("openId: "+ opendid +" \t文字推送失败：" + jsonObject.get("errmsg").toString());
            return false;
        }
    }

    public static String keFuSendByDesc(String desc) {
        return WxKefuJson.keFuJson.replace("CONTENT", desc);
    }

    /**
     * 客服消息推送
     */
    public static boolean keFuImagesSendByOpenId(String opendid, String desc) throws IOException {
        JSONObject jsonObject = go(opendid, desc);
        if (jsonObject.get("errcode").toString().equals("0")) {
            logger.info("-------------------推送图片 success ----"+ opendid +"----------------");
            return true;
        } else {
            logger.error("openId: "+ opendid +" \t图片推送失败：" + jsonObject.get("errmsg").toString());
            return false;
        }
    }

    /**
     * 客服消息推送
     */
    public static boolean keFuHourSendByOpenId(String opendid, String desc) throws IOException {
        String txt = WxKefuJson.keFuJson.replace("CONTENT", desc);
        JSONObject jsonObject = go(opendid, txt);
        if (jsonObject.get("errcode").toString().equals("0")) {
            logger.info("-------------------推送24小时消息 success --------------------");
            return true;
        } else {
            logger.error("openId: "+ opendid +" \t24小时消息推送失败：" + jsonObject.get("errmsg").toString());
            return false;
        }
    }

    // todo ceyixia
    public static String keFuImagesSendByDesc(String mediaId) {
        return WxKefuJson.KEFUIMAGE.replace("MEDIA_ID", mediaId);
    }

    private static JSONObject go(String opendid, String desc) throws IOException {
        String token = HttpUtils.doGet("https://one.wcwlvip.com/redis/get");
        String url = WxKefuJson.keFuUrl.replace("ACCESS_TOKEN", token);
        String json = desc.replace("OPENID", opendid);
        String result = doPostJson(url, json);
        return JSONObject.parseObject(result);
    }



    public static void main(String[] args) {

    }

}
